package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.npc.message.RespStartGather;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.utils.Const;
import com.kitty.listener.BaseGameEvent;
import com.kitty.mina.message.MessagePusher;

/**采集目标处理类*/
public class GatherTargetHandler extends TaskTargetHandler {
    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        /**处理采集完成*/
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        TaskSet taskSet = taskService.getTaskSet(role.getTask().getGatherTaskId(), role);
        /**找不到采集对应任务*/
        if (taskSet == null) {return ;}
        role.getTask().setGatherTaskId(-1);

        TaskTarget roleDoingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
        if (roleDoingTaskTarget == null) {return ;}

        if (!taskService.isMatchTargetType(roleDoingTaskTarget, TaskTargetType.GATHER)) {return ;}

        taskService.updateTargetProgress(role, taskSet, roleDoingTaskTarget);
    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        /**记录采集进度条由当前任务触发*/
        if (SpringUtils.getBean(NewTaskService.class).getTaskSet(role.getTask().getGatherTaskId(), role) != null) {
            return ;
        }
        role.getTask().setGatherTaskId(taskSet.getId());


        /**显示采集进度条动画*/
        role.setGatherType(Const.GATHER_TYPE_PLANT);
        int currtime = ((int)System.currentTimeMillis()/1000);
        RespStartGather respStartGather = new RespStartGather();
        respStartGather.setStartTime(currtime);
        respStartGather.setEndTime(currtime + 3);
        respStartGather.setMsg("采集");
        respStartGather.setGather_style("default");
        MessagePusher.pushMessage(role, respStartGather);
    }
}
